package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.RoutineSlot;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoutineSlotMapper {

    @Select("SELECT * FROM routine_slots WHERE user_id=#{userId} ORDER BY sort_order ASC, id ASC")
    List<RoutineSlot> findByUser(Long userId);

    @Insert("INSERT INTO routine_slots (user_id, label, time_range, description, emoji, background_path, sort_order) " +
            "VALUES (#{userId}, #{label}, #{timeRange}, #{description}, #{emoji}, #{backgroundPath}, #{sortOrder})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(RoutineSlot slot);

    @Update("UPDATE routine_slots SET label=#{label}, time_range=#{timeRange}, description=#{description}, " +
            "emoji=#{emoji}, background_path=#{backgroundPath}, sort_order=#{sortOrder} WHERE id=#{id} AND user_id=#{userId}")
    int update(RoutineSlot slot);

    @Delete("DELETE FROM routine_slots WHERE id=#{id} AND user_id=#{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT * FROM routine_slots WHERE user_id=#{userId} ORDER BY sort_order ASC, id ASC LIMIT #{limit} OFFSET #{offset}")
    List<RoutineSlot> findPage(@Param("userId") Long userId,
                               @Param("offset") int offset,
                               @Param("limit") int limit);

    @Select("SELECT COUNT(1) FROM routine_slots WHERE user_id=#{userId}")
    long countByUser(Long userId);
}

